package com.chris.libs.base.x.mvpp.recycler;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by Chris Chen on 2017/7/3 17:18.
 * Explain:RecyclerView的Adapter基类
 */

public abstract class BaseVLayoutAdapter<D, H extends BaseVLayoutViewHolder<D>> extends DelegateAdapter.Adapter<H> {
    protected String tag = "TAG";
    protected Context context;
    protected List<D> dataList;
    protected View itemView;
    protected OnItemClickListener onItemClickListener;
    private List<H> viewHolderList = new ArrayList<>();

    public BaseVLayoutAdapter(Context context) {
        this.context = context;
        this.dataList = new ArrayList<>(16);
    }

    public BaseVLayoutAdapter(Context context, OnItemClickListener onItemClickListener) {
        this.context = context;
        this.dataList = new ArrayList<>(16);
        this.onItemClickListener = onItemClickListener;
    }

    public BaseVLayoutAdapter(String tag, Context context, List<D> dataList) {
        this.tag = tag;
        this.context = context;
        this.dataList = dataList;
    }

    public BaseVLayoutAdapter(String tag, Context context, List<D> dataList, OnItemClickListener onItemClickListener) {
        this.tag = tag;
        this.context = context;
        this.dataList = dataList;
        this.onItemClickListener = onItemClickListener;
    }

    public BaseVLayoutAdapter(String tag, Context context, D[] datas) {
        this.tag = tag;
        this.context = context;
        this.dataList = Arrays.asList(datas);
    }

    public BaseVLayoutAdapter(String tag, Context context, D[] datas, OnItemClickListener onItemClickListener) {
        this.tag = tag;
        this.context = context;
        this.dataList = Arrays.asList(datas);
        this.onItemClickListener = onItemClickListener;
    }

    public BaseVLayoutAdapter(String tag, Context context) {
        this.tag = tag;
        this.context = context;
        this.dataList = Collections.emptyList();
    }

    public BaseVLayoutAdapter(String tag, Context context, OnItemClickListener onItemClickListener) {
        this.tag = tag;
        this.context = context;
        this.dataList = Collections.emptyList();
        this.onItemClickListener = onItemClickListener;
    }

    public BaseVLayoutAdapter(Context context, List<D> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    public BaseVLayoutAdapter(Context context, List<D> dataList, OnItemClickListener onItemClickListener) {
        this.context = context;
        this.dataList = dataList;
        this.onItemClickListener = onItemClickListener;
    }

    public BaseVLayoutAdapter(Context context, D[] datas) {
        this.context = context;
        this.dataList = Arrays.asList(datas);
    }

    public BaseVLayoutAdapter(Context context, D[] datas, OnItemClickListener onItemClickListener) {
        this.context = context;
        this.dataList = Arrays.asList(datas);
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onBindViewHolder(@NonNull H holder, int position) {
        holder.getItemView().setOnClickListener(view -> {
            if (this.onItemClickListener != null) {
                this.onItemClickListener.onItemClick(this.tag, this.itemView, position, getItem(position), null, null);
            }
        });

        refreshView(holder, position);
    }

    @NonNull
    @Override
    public H onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        this.itemView = LayoutInflater.from(context).inflate(layoutId(), parent, false);
        H viewHolder = createViewHolder(this.itemView);
        this.viewHolderList.add(viewHolder);
        return viewHolder;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    /**
     * 布局文件
     *
     * @return
     */
    protected abstract int layoutId();

    /**
     * 创建ViewHolder
     *
     * @param itemView
     * @return
     */
    protected abstract H createViewHolder(View itemView);

    /**
     * 刷新数据
     *
     * @param holder
     * @param position
     */
    protected void refreshView(H holder, int position) {
        refreshItemView(holder, getItem(position));
    }

    /**
     * 刷新列表项
     *
     * @param holder
     * @param item
     */
    protected abstract void refreshItemView(H holder, D item);

    public List<H> getViewHolderList() {
        return viewHolderList;
    }

    /**
     * 找到对应的ViewHolder
     *
     * @param position
     * @return
     */
    public H findViewHolderAt(int position) {
        return this.viewHolderList.get(position);
    }

    @Override
    public int getItemCount() {
        return null == this.dataList ? 0 : this.dataList.size();
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    /**
     * 获取列表项数据
     *
     * @param position
     * @return
     */
    public D getItem(int position) {
        return null == this.dataList ? null : this.dataList.get(position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 更新数据
     *
     * @param dataList
     */
    public void updateData(List<D> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public void updateData(D[] datas) {
        this.dataList = Arrays.asList(datas);
        notifyDataSetChanged();
    }

    /**
     * 加载更多
     *
     * @param dataList
     */
    public void loadMore(List<D> dataList) {
        if (null == this.dataList) {
            this.dataList = new ArrayList<>(16);
        }
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }

    /**
     * 加载更多
     *
     * @param datas
     */
    public void loadMore(D[] datas) {
        if (null == this.dataList) {
            this.dataList = new ArrayList<>(16);
        }
        this.dataList.addAll(Arrays.asList(datas));
        notifyDataSetChanged();
    }

    /**
     * 列表项点击事件监听
     * todo 列表项内部事件分发需要扩展
     */
    public interface OnItemClickListener {
        void onItemClick(String tag, View itemView, int position, Object item, View childView, Object childDataValue);
    }
}

